import React, { Component } from 'react';
import { observer, inject } from 'mobx-react';
import { withRouter } from 'react-router-dom';
import CardHearder from '../../public/CardHearder';
import { RouteLayout } from 'weaver-mobile-page';
import { Accordion, List, InputItem, WingBlank, Flex, WhiteSpace, Card, ActivityIndicator, Tools } from 'weaver-mobile';
const { FormItem, Item } = List;
const isIPhone = new RegExp('\\biPhone\\b|\\biPod\\b', 'i').test(window.navigator.userAgent);
let wrapProps;
if (isIPhone) {
  wrapProps = {
    onTouchStart: e => e.preventDefault(),
  };
}
const ButtonGroup = RouteLayout.ButtonGroup;
const getLabel = Tools.getLabel;
@inject('commerceCardStore')
@withRouter
@observer
export default class main extends Component {
  constructor(props) {
    super(props);
  }

  componentWillMount() {
    const { commerceCardStore, match,params } = this.props;
    const ps = params ? params : match.params;
    commerceCardStore.doNew(ps);
  }

  componentDidMount() {
    const { commerceCardStore } = this.props;
    commerceCardStore.doInit();
  }
  componentWillUnmount() {
    const { commerceCardStore } = this.props;
    commerceCardStore.clearBussnessInfo();
  }
  render() {
    const store = this.props.commerceCardStore;
    const { initResponsed, initSuccessed, initFailedMsg, APIresult } = store;
    const defaultActKey = "";
    let items = [];
    const buttonGroupData = [//按钮组
      {
        key: 'back',
        text: getLabel('1290','返回'),
        onClick: () => this.props.history.go(-1),
        type: 'primary',
      },
    ];
    if (initResponsed&&initSuccessed) {
      if ( APIresult) {
        const { businessInfoData } = store;
        const emp = businessInfoData.employees;//主要成员
        let empItem = [];
        if (emp) {
          emp.forEach((employee, i) => {
            empItem.push(
              <div key={i + "emp"}>
                <div>
                  <Card >
                    <Card.Header
                      title={<div className={"crm-form-card-header"}>{employee.name}</div>}
                    />
                    <Card.Body>
                      <WhiteSpace size="lg" />
                      <div>{getLabel('357','职务')+"：" + employee.job_title}</div>
                    </Card.Body>
                  </Card>
                </div>
              </div>
            );
          })
        }
        const partners = businessInfoData.partners;//股东信息
        let partnerItem = [];
        if (partners) {
          partners.forEach((partner, i) => {
            let totalShouldCapi = 0;
            let totalRealCapi = 0;
            let real_capi_items = partner.real_capi_items;
            let should_capi_items = partner.should_capi_items;

            if (real_capi_items) {//实缴出资额
              real_capi_items.forEach((real_capi_item) => {
                let item = real_capi_item.real_capi.substring(0, real_capi_item.real_capi.length - 2);
                if (item > 0) {
                  totalRealCapi = Number(totalRealCapi) + Number(item);
                }
              }
              );
            }
            if (should_capi_items) {//应缴出资额
              should_capi_items.forEach((should_capi_item) => {
                let item = should_capi_item.shoud_capi.substring(0, should_capi_item.shoud_capi.length - 2);
                if (item > 0) {
                  totalShouldCapi = Number(totalShouldCapi) + Number(item);
                }
              }
              );
            }
            partnerItem.push(
              <Card key={i}>
                <Card.Header
                  title={<div className={"crm-form-card-header"}>{partner.name}</div>}
                />
                <Card.Body>
                  <WingBlank>
                    <div>{getLabel('390924',"股东类型")+"：" + partner.stock_type}</div>
                    <WhiteSpace size="sm" />
                    <div>{getLabel('23787',"证件类型")+"：" + partner.identify_type}</div>
                    <WhiteSpace size="sm" />
                    <div>{getLabel('390925','证件编号')+"：" + partner.identify_no}</div>
                    <WhiteSpace size="sm" />
                    <div>{getLabel('390926','认缴出资额')+"：" + (totalShouldCapi > 0 ? totalShouldCapi : "-") + getLabel('84393','万元')}</div>
                    <WhiteSpace size="sm" />
                    <div>{getLabel('390927','实缴出资额')+"：" + (totalRealCapi > 0 ? totalRealCapi : "-") + getLabel('84393','万元')}</div>
                  </WingBlank>
                </Card.Body>
              </Card>
            );
          });
        }
        const branches = businessInfoData.branches;//分支机构
        let brancheItem = [];
        if (branches) {
          branches.forEach((branche, i) => {
            let itemProps = {
              name: branche.name
            }
            brancheItem.push(
              <Item
                key={i}
              >
                {itemProps.name}
              </Item>
            );
          }
          );
        }
        const changerecords = businessInfoData.changerecords;//变更记录
        let changerecordItem = [];

        if (changerecords) {
          changerecords.forEach((changerecord, i) => {
            let changeItem = changerecord.change_item
            if (changerecord.change_item.search("出资") > -1) {
              changeItem = getLabel('390929',"出资情况");
            }
            if (changerecord.change_item.search("地址") > -1) {
              changeItem = getLabel('110',"地址");
            }
            if (changerecord.change_item.search("经营范围") > -1) {
              changeItem = getLabel('31031',"经营范围");
            }
            if (changerecord.change_item.search("注册资本") > -1) {
              changeItem = getLabel('20668',"注册资本");
            }
            if (changerecord.change_item.search("股东情况") > -1) {
              changeItem = getLabel('391023',"股东情况");
            }
            if (changerecord.change_item.search("企业名称") > -1) {
              changeItem = getLabel('18415',"企业名称");
            }
            changerecordItem.push(
              <div key={i} >
                <WingBlank>
                  <WhiteSpace size="sm" />
                  <Flex className="crm_commerce_change_head">
                    <WingBlank><div className="crm_commerce_change_head_font"> {changeItem}</div></WingBlank>
                    <WingBlank><div className="crm_commerce_change_head_font" style={{float:"right"}}>{changerecord.change_date}</div></WingBlank>
                  </Flex>
                  <WhiteSpace size="sm" />
                  <div className='crm_commerce_change_body'>
                    <Flex align="start" className='crm_commerce_change_tip'>
                      <Flex.Item wrap="true">{getLabel('130779','变更前内容')}</Flex.Item>
                      <Flex.Item wrap="true">{getLabel('130781','变更后内容')}</Flex.Item>
                    </Flex>
                    <WhiteSpace size="lg" />
                    <Flex align="start">
                      <Flex.Item wrap="true">{changerecord.before_content}</Flex.Item>
                      <Flex.Item wrap="true">{changerecord.after_content}</Flex.Item>
                    </Flex>
                  </div>
                </WingBlank >
              </div >
            );
          }
          );
        }

        const abnormal_items = businessInfoData.abnormal_items;//经营信息
        let abnormalitems = [];
        let abnormalDataSource = [];
        if (abnormal_items) {
          abnormal_items.forEach((abnormal_item, i) => {
            abnormalDataSource.push(
              <div key={i} >
                <div className="crm_commerce_abnormal_body">
                  <FormItem
                    label={getLabel('130785','列入日期')+":"}
                    wrap="nowrap"
                    line
                    editable="false"
                    content={(<InputItem value={abnormal_item.in_date} />)}
                  />
                  <FormItem
                    label={getLabel('130784','经营异常列入原因')+":"}
                    wrap="nowrap"
                    editable="false"
                    content={(<InputItem value={abnormal_item.in_reason} />)}
                  />
                  <FormItem
                    label={getLabel('130787','移出时间')+":"}
                    wrap="nowrap"
                    line
                    editable="false"
                    content={(<InputItem value={abnormal_item.out_date} />)}
                  />
                  <FormItem
                    label={getLabel('130786','移出原因')+":"}
                    wrap="nowrap"
                    editable="false"
                    content={(<InputItem value={abnormal_item.out_reason} />)}
                  />
                </div>
                <div className="crm_commerce_abnormal_space"><WhiteSpace size="xl" /></div>
              </div>
            );
          });
        }
        return (
          <div className={"crm-form-main"}>
            <div className="crm-form-content">
              <CardHearder
                title1={businessInfoData.name} //公司名
                title2={getLabel('27843','法定代表人')+":" + businessInfoData.oper_name+" , "+getLabel('27319','成立日期')+":" + businessInfoData.start_date}//法定代表人,成立日期
                title3={getLabel('27317','注册资金')+":" + businessInfoData.regist_capi}// 注册资金
              />
              <Accordion accordion defaultActiveKey="0" className="my-accordion">
                <Accordion.Panel header={getLabel('390931',"登记信息")} className="pad">
                  <List>
                    <FormItem
                      label={getLabel('130767','企业注册号')+":"}
                      wrap="nowrap"
                      editable="false"
                      content={(<InputItem value={businessInfoData.reg_no} />)}
                    />
                    <FormItem
                      label={getLabel('130774','组织机构号')+":"}
                      wrap="nowrap"
                      editable="false"
                      content={(<InputItem value={businessInfoData.org_no} />)}
                    />
                    <FormItem
                      label={getLabel('130775','统一社会信用代码')+":"}
                      wrap="nowrap"
                      editable="false"
                      content={(<InputItem value={businessInfoData.credit_no} />)}
                    />
                    <FormItem
                      label={getLabel('390933','经营状态')+":"}
                      wrap="nowrap"
                      editable="false"
                      content={(<InputItem value={businessInfoData.status} />)}
                    />
                    <FormItem
                      label={getLabel('32497','企业类型')+":"}
                      wrap="nowrap"
                      editable="false"
                      content={(<InputItem value={businessInfoData.econ_kind} />)}
                    />
                    <FormItem
                      label={getLabel('31133','住所')+":"}
                      wrap="nowrap"
                      editable="false"
                      content={(<InputItem value={businessInfoData.address} />)}
                    />
                    <FormItem
                      label={getLabel('31059','营业期限')+":"}
                      wrap="nowrap"
                      editable="false"
                      content={(<InputItem value={businessInfoData.start_date + "到" + businessInfoData.term_end} />)}
                    />
                    <FormItem
                      label={getLabel('130772','核准日期')+":"}
                      wrap="nowrap"
                      editable="false"
                      content={(<InputItem value={businessInfoData.check_date
                      } />)}
                    />
                    <FormItem
                      label={getLabel('125912','登记机关')+":"}
                      wrap="nowrap"
                      editable="false"
                      content={(<InputItem value={businessInfoData.belong_org} />)}
                    />
                    <FormItem
                      label={getLabel('31031','经营范围')+":"}
                      wrap="nowrap"
                      editable="false"
                    />
                    <WingBlank>
                      <Item wrap>{businessInfoData.scope}</Item>
                    </WingBlank>
                  </List>
                </Accordion.Panel>
                <Accordion.Panel header={getLabel('27342','股东信息')} className="pad">
                  <List>
                    {partnerItem}
                  </List>
                </Accordion.Panel>
                <Accordion.Panel header={getLabel('130761','主要成员')} className="pad">
                  <List>
                    {empItem}
                  </List>
                </Accordion.Panel>
                <Accordion.Panel header={getLabel('130762','分支机构')} className="pad">
                  <List>
                    {brancheItem}
                  </List>
                </Accordion.Panel>
                <Accordion.Panel header={getLabel('130763','经营信息')} className="pad">
                  <List>
                    {abnormalDataSource}
                  </List>
                </Accordion.Panel>
                <Accordion.Panel header={getLabel('19765','变更记录')} className="pad">
                  {changerecordItem}
                </Accordion.Panel>
              </Accordion>

            </div>
            <div><ButtonGroup data={buttonGroupData} /></div>
          </div>
        );
      } else {
        // 接口调用不成功的提示信息
        return<div>
          <div>
          {initFailedMsg}
          </div>
          <div>
            <ButtonGroup data={buttonGroupData} />
          </div>
        </div>;
      }
    } else {
      return <div className="crm-bg-loading"><ActivityIndicator text="Loading..."/></div>;
    }
  }
}